<?php

	/////////////////////////////
	// Localhost ? Production ?
	//////////////////////////////
	
	$whitelist = array('127.0.0.1','localhost');
	$isLocal = false;
	if(in_array($_SERVER['REMOTE_ADDR'], $whitelist) || in_array($_SERVER['SERVER_NAME'], $whitelist)) $isLocal = true;
	
	if (!$isLocal) {
	
		/////////////////////////////
		// Configuration App Facebook
		//////////////////////////////
		$isConnected = false;
		$isLike = false; 
		
		$app_id = "497500026935487";
		
		$page_id = "191131100938419";
		
		$scope = "email,publish_stream,user_photos";
		
		$tab_page = "https://www.facebook.com/pages/null/" . $page_id . "?sk=app_" . $app_id;
		
		$auth_url = "https://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($tab_page) . "&scope=email";
		
		$signed_request = $_REQUEST["signed_request"];
		
		list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
		
		$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
		
		if (empty($data["user_id"])) {
			 echo("<script> window.top.location.href='" . $auth_url . "'</script>");
		} else {
			$isConnected = true;
		}
		if ($data["page"]["liked"] == 1) $isLike = true;
		
		///////////////////////////// 
		/////////////////////////////
	
	}

?>

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Backbook</title>
  <meta name="description" content="">
  <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700,300' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="css/bootstrap.min.css">
  <link rel="stylesheet" href="css/style.css">
</head>

 

 <body> 	

	

	<div id="fb-root"></div>

	<?php if ($isLocal || $isConnected) { ?>

<!-- HTML Structure -->
	<div id="loading"><img src="images/loading.gif" width="32" height="32"/></div>
    <header>
    <h1>Facebook API</h1>
    </header>
    <div id="wrapper">
    </div>
    <footer>
    </footer>
<!-- HTML Structure -->

	<?php } ?>

	<?php if (!$isLocal && !$isLike) { ?>

	<div id="no-like">Vous n'êtes pas fan de la page</div>

	<?php } ?>

<!-- Libs -->
<script src="js/libs/jquery-1.9.1.min.js"></script>
<script src="js/libs/bootstrap.min.js"></script>
<script src="js/libs/head.load.min.js"></script>
<script src="js/libs/underscore-min.js"></script>
<script src="js/libs/backbone-min.js"></script>
<script src="js/libs/facebook/FacebookAPI.js"></script>
<script src="js/libs/json2.js"></script>
<script src="js/utils.js"></script>
<!-- Start -->	
<script src="js/start.js"></script>

<?php if (!$isLocal && $isLike && $isConnected) { ?>

<script>

  window.fbAsyncInit = function() {

    FB.init({

      appId: <?php echo $app_id ?>,

      cookie: true,

      xfbml: true,

      oauth: true

    });

	FB.login(function(response) {

	   if (response.authResponse) {
		   setAuthResponse(response.authResponse);
		   facebookComplete();
	   }

	   else facebookError();

	 },{scope: '<?php echo $scope ?>'});

	/*FB.Canvas.setSize({ width: 810, height: 550 });  */   

  };

  (function() {

    var e = document.createElement('script'); e.async = true;

    e.src = document.location.protocol +

      '//connect.facebook.net/fr_FR/all.js';

    document.getElementById('fb-root').appendChild(e);

  }());

</script>

<?php } else { ?>

<script>
facebookComplete(<?php echo $isLocal ?>);
</script>

<?php } ?>

</body>

</html>